package com.spider.moudle.util;

import com.spider.moudle.entity.Area;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具类
 */
public class CommonUtil {

	private static Pattern pattern = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");

	/**
	 * 验证参数是否不为空,非空true
	 * @param object
	 * @return
	 */
	public static boolean isNotNull(Object object) {
		if(object == null) {
			return false;
		}
		if(object instanceof String) {
			return !"".equals((String)object);
		} else if(object instanceof List) {
			return !((List) object).isEmpty();
		} else if(object instanceof Map) {
			return !((Map)object).isEmpty();
		} else {
			return true;
		}
	}

	/**
	 * 验证参数是否为空,空true
	 *
	 * @param object
	 * @return
	 */
	public static boolean isNull(Object object) {
		if(object == null) {
			return true;
		}
		if(object instanceof String) {
			return "".equals((String)object);
		} else if(object instanceof List) {
			return ((List) object).isEmpty();
		} else if(object instanceof Map) {
			return ((Map)object).isEmpty();
		} else {
			return false;
		}
	}


	/**
	 * 生成随机的32位uuid
	 * @return
	 */
	public final static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 去掉字符串中的空格(包括中文空格)
	 * @param content
	 * @return
	 */
	public static String trim(String content) {
		return content.replace("　", "").trim();
	}

	/**
	 * 去掉内容中所有的html标签
	 * @param content 要过滤标签的内容
	 * @return
	 */
	public static String filtHtml(String content){
		Matcher m = pattern.matcher(content);
		List<String> srcs = new ArrayList<>();
		while(m.find()){
			srcs.add(m.group(1));
		}
		String regex = "<[^>]*>";
		String str = content.replaceAll(regex, "");
		str = str.replace("<font>", "")
				.replace("</font>", "")
				.replace("<p>", "")
				.replace("</p>", "")
				.replace("<span>", "")
				.replace("</span>", "")
				.replace("<li>", "")
				.replace("</li>", "");
		return str;
	}

	/**
	 * 处理爬虫数据，主要包括去掉字符串中的空格和html标签
	 * @param content
	 * @return
	 */
	public static String handle(String content) {
		content = CommonUtil.trim(content);
		content = CommonUtil.filtHtml(content);
		return content;
	}


	/**
	 * @param start     开始页
	 * @param end       结束页
	 * @param threadNum 线程数量
	 * @return 分配好后的结果
	 * @author 夏茂轩
	 * @date 2017/1/15 22:36
	 * @description 分页任务分配器
	 */
	public static ArrayList<ArrayList<Integer>> splitPages(int start, int end, int threadNum) {
		//检查参数
		ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<ArrayList<Integer>>();
		//平均每份多少个
		int part = (int) Math.ceil((double) (end - start + 1) / threadNum);
		int total = end - start + 1;
		if (threadNum >= total) {
			threadNum = total;
		}
		for (int i = 0; i < threadNum - 1; i++) {
			//分给每一份
			ArrayList<Integer> pages = new ArrayList<>();
			for (int k = 0; k < part; k++) {
				pages.add(start + i * part + k);
			}
			arrayLists.add(pages);
		}
		//分剩下的多出来的任务
		int rest = total - part * (threadNum - 1);
		int restStart = start + part * (threadNum - 1) - 1;
		ArrayList<Integer> pages = new ArrayList<>();
		for (int i = 1; i < rest + 1; i++) {
			pages.add(restStart + i);
		}
		if (pages.size() > 0) {
			arrayLists.add(pages);
		}
		return arrayLists;
	}

}
